// index.js
import ProcessActionPromiseFactory from "../../utils/promise.js";
Page({
  data: {
    isShowing:true,   //是否显示头部功能栏组件
    themeList:[],     //主题css名称列表
    themeText:['春季','夏季','秋季','冬季','四季'],//主题文字
    navigatorList:{   //导航跳转地址列表
      '互动':{
        '所有产品':'/pages/goodsList/goodsList',
        '艺术推荐':'/pages/actRecommend/actRecommend',
        '画廊':'/pages/gallery/gallery',
        '活动':'/pages/activity/activity'
      },
      '介绍':{
        '关于':'/pages/about/about',
        '支持我们':'/pages/support/support'
      },
      '个人':{
        '购物车':'/pages/shopcart/shopcart',
        '我的账户':'/pages/account/account',
        '订单':'/pages/order/order'
      }
    },
    productSelNameList:[    //产品选择名称表
      '艺术画作',
      '明信卡',
      '花瓶'
    ],
    productSelImgList:[     //产品选择图片表
      (getApp()).globalData.hostAddress+'/gallaryMarket/img/goodsChoose/choose1.png',
      (getApp()).globalData.hostAddress+'/gallaryMarket/img/goodsChoose/choose2.png',      
      (getApp()).globalData.hostAddress+'/gallaryMarket/img/goodsChoose/choose3.png'  
    ],
    productSelListIndex:0,    //产品选择表索引
    founder:[                   //创始信息
      {
        imgUrl:(getApp()).globalData.hostAddress+'/gallaryMarket/img/team.png',
        text:'作为网上画廊商城平台的创始团队，我们是一群对艺术充满热爱、对市场有着敏锐洞察力的专业人士。我们深知，在数字时代，艺术品的价值不应被地域或时间所限制，而应通过互联网的力量，让更多人欣赏到、收藏到，甚至投资到这些珍贵的艺术品。我们团队汇集了来自艺术、技术、市场等多领域的精英，他们不仅拥有深厚的行业背景与专业知识，更具备创新思维与执行力。我们致力于打造一个安全、透明、便捷的在线画廊商城，让每一件艺术品都能找到它的知音，让每一位用户都能在这里发现价值、创造价值。我们相信，通过我们的努力，艺术品的商业价值将得到充分的展现与提升，我们也将为艺术产业的发展贡献自己的一份力量。'
      }
    ]
  },
  //bindTapProductSel 绑定点击产品分类函数
  bindTapProductSel(e){
    this.setData({
      productSelListIndex:e.currentTarget.dataset.id
    });
  },
  //bindTapProSelNav 产品选择导航函数
  bindTapProSelNav(){
    let index = this.data.productSelListIndex;
    (getApp()).globalData.preTypeIndex = index+=1;
    wx.navigateTo({
      url: '/pages/goodsList/goodsList',
    });
  },
  //bindTapThemeChange 点击更换主题响应函数
  bindTapThemeChange(e){
    var newIndex = +e.detail.value;
    this.setData({
      themeIndex:newIndex
    });
    (getApp()).globalData.themeIndex = newIndex;
  },
  //bindTapNavToActRec 点击跳转到艺术推荐页响应函数
  bindTapNavToActRec(){
    if((getApp()).isLogined() == 0) return;//检查是否登录
    wx.navigateTo({
      url: '/pages/actRecommend/actRecommend',
    });
  },
  //bindTapNavToGoodList 点击跳转到商品列表函数
  bindTapNavToGoodList(){
    console.log(this.data.productSelListIndex);
  },
  //bindTapNavToGoodDetail 点击跳转到详情页响应函数
  bindTapNavToGoodDetail(e){
    if((getApp()).isLogined() == 0) return;//检查是否登录
    var gid = +e.target.dataset.gid;
    var data = this.data.bestSeller.concat(this.data.moreProducts);
    var goodData = data.filter((item)=>item.gid == gid);
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail',
      success: function(res) {
        res.eventChannel.emit('goodData', { data: goodData })
      }
    });
  },
  //bindTapAddShopcart 点击添加商品到购物车响应函数
  bindTapAddShopcart(e){
    var gid = +e.target.dataset.gid;
    var app = getApp();
    //检查是否登录
    if(app.isLogined() == 0) return;
    //检查是否已经添加了
    var isExsit = false;
    app.globalData.shopcart.forEach((item)=>{
      if(item.gid == gid) isExsit = true;
    });
    if(isExsit){
      wx.showToast({
        title: '已存在',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    //执行添加操作
    app.pinia('addShopCart',{
      cid:app.globalData.clientInfo.cid,
      gid:gid,
      num:1,
      priceIndex:0
    },(res)=>{
      app.globalData.shopcart=res.data;
      this.setData({shopcartNum:this.data.shopcartNum+=1});
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 1000
      });
    });
  },
  //bindTapNavigator 点击导航栏跳转响应函数
  bindTapNavigator(e){
    if((getApp()).isLogined() == 0) return;//检查是否登录
    var theUrl = e.target.dataset.url;
    wx.navigateTo({
      url: theUrl,
    });
  },
  //appInit 小程序初始化函数
  appInit(){
    //小程序初始化
    var app = getApp();
    var that = this;
    //获取页面配置数据
    app.pinia("getPageConfigData",{},(res)=>{
      var data = res.data;
      console.log("configdata:",data);
      app.globalData.themeIndex = data.basic.themeIndex;
      app.globalData.galleryPic = data.basic.artt;
      that.setData({
        themeIndex:data.basic.themeIndex,
        arttPicUrl:data.basic.artt,
        bestSeller:data.hot,
        moreProducts:data.more
      });
    });
    //登录后获取购物车列表
    ProcessActionPromiseFactory((resolve,reject)=>{
      //先自动登录获取cid
      if(wx.getStorageSync('gallerySuperMarketToken')!=''){
        app.pinia('autoLogin',{
          openid:wx.getStorageSync('gallerySuperMarketToken')
        },(res)=>{
          resolve(res.data.cid);
        });
      }
    })
    .then((result)=>{
      //通过cid获取用户信息
      return ProcessActionPromiseFactory((resolve,reject)=>{
        let cid = result;
        app.pinia('checkBasicInfo',{
          'cid':cid
        },(res)=>{
          console.log("clientInfo:",res.data);
          if(res.data.openid==undefined) reject('ClientInfoError');
          app.globalData.clientInfo = res.data;//更新全局
          resolve(res.data.cid);
        });
      });
    })
    .then((result)=>{
      //通过cid获取用户购物车
      return ProcessActionPromiseFactory((resolve,reject)=>{
        let cid = result;
        app.pinia('checkShopCart',{cid:cid},(res)=>{
          if(res.data.filter==undefined) reject('ShopcartError');
          app.globalData.shopcart = res.data;//更新全局
          that.setData({shopcartNum:app.globalData.shopcart.length});//获取购物车数量          
        })
      });
    })
    .catch((err)=>{
      console.log("error message:"+err);
    });
  },
  onLoad(){
    this.setData({themeIndex:(getApp()).globalData.themeIndex});  //设置本地的默认主题样式
    this.appInit();                                               //初始化小程序
  },
  onShow(){
    this.setData({themeList:(getApp()).globalData.themeList});    //设置主题列表
  }
})
